package com.qidianit.mes.production.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidianit.mes.common.pojo.MesResult;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.production.mapper.ProcessMapper;
import com.qidianit.mes.production.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2025/8/4 9:52
 * @description
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process> implements ProcessService {

    @Value("${PROCESS_CODE_PRE}")
    private String processCodePre;
    @Value("${PROCESS_CODE_START}")
    private String processCodeStart;
    @Value("${PROCESS_CODE_KEY}")
    private String processCodeKey;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public MesResult<Integer> deleteBatch(String[] processCodes) {

        UpdateWrapper< Process> wrapper = new UpdateWrapper<>();
        wrapper.in("process_code", processCodes);
        wrapper.set("delete_flag", 1);
        return MesResult.success(this.baseMapper.update(null,wrapper));
    }

    @Override
    public MesResult<Integer> removeProcess(String processCode) {
        MesResult<Process> processById = this.getProcessById(processCode);
        if (processById.getData() != null) {
            Process data = processById.getData();
            data.setDeleteFlag(1);// 逻辑删除
            return this.updateById(data) ? MesResult.success(1) : MesResult.error();
        }
        return MesResult.error(300,"要删除的数据不存在");
    }

    @Override
    public MesResult<Integer> updateProcess(Process process) {
        process.setModifyTime(new Date());
        return this.updateById(process) ? MesResult.success(1) : MesResult.error();
    }

    @Override
    public MesResult<Process> getProcessById(String processId) {
        return MesResult.success(this.getById(processId));
    }

    @Override
    public MesResult<Integer> saveProcess(Process process) {
        return this.save(process)?MesResult.success(1):MesResult.error();
    }

    @Override
    public MesResult<String> getProcessCode() {
        // 判断是否已经有值
        if(stringRedisTemplate.hasKey(processCodeKey)){
            // 如果存在直接通过increment获取并且返回
            return MesResult.success(processCodePre + stringRedisTemplate.opsForValue().increment(processCodeKey).toString());
        }
        // Redis中不存在就存储起始值，并且返回
        stringRedisTemplate.opsForValue().set(processCodeKey, processCodeStart);
        return MesResult.success(processCodePre + processCodeStart);
    }

    @Override
    public MesResult<Page<Process>> listProcess(Process process, int pageNum, int pageSize) {
        // 准备分页对象
        Page<Process> page = new Page<>(pageNum, pageSize);
        // 封装查询条件
        QueryWrapper<Process> wrapper = new QueryWrapper<>();
        wrapper.eq(process != null && process.getProcessCode() != null && !process.getProcessCode().isEmpty(), "process_code", process.getProcessCode());
        wrapper.like(process != null && process.getProcessName() != null && !process.getProcessName().isEmpty(), "process_name", process.getProcessName());
        wrapper.eq(process != null && process.getIsEnabled() != null, "is_enabled", process.getIsEnabled());
        // 分页查询  。这里直接调用父类中封装好的方法
        page = this.page(page, wrapper);
        return MesResult.success(page);
    }
}
